﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;


public class SkillManager : NetworkBehaviour {

    private Vector3 mousePos;
    private GameObject gridGroup = null;
    private GameObject mousePointObj = null;
    private string mousePointObjName = "";

    private Image Loadingbar;
    private LayerMask mask;
    //   public int BombForce = 300;
    //   public float rangeNumber = 10.0f;
    public int DamageNmerical = 100;//技能伤害
    public int takeMoney = 50;
    public int CDtime = 10;
    private float time = 0;

    public static SkillManager instance = null;

    public void Awake()
    {
        instance = this;
    }
    public void Update()
    {
        if (GameObject.FindWithTag("Loadingbar") != null)
        {
            Loadingbar = GameObject.FindWithTag("Loadingbar").GetComponent<Image>();
        }
        if (Loadingbar != null)
        {
            if (Loadingbar.fillAmount < 1)
            {
                CDccaculate();
            }
            else
                time = 0;
        }
    }

    private void CDccaculate()
    {
        time += Time.deltaTime;
        Loadingbar.fillAmount = time / CDtime;
    }
    public void BombSkill(ref bool BombSkillisActive)
    {
        if (gridGroup == null)
        {
            gridGroup = Instantiate(transform.GetComponent<Camp>().Grids, transform.GetComponent<Camp>().Grids.transform.position, transform.GetComponent<Camp>().Grids.transform.rotation);
        }
        gridGroup.SetActive(true);

        // 发射射线检测层级碰撞
        RaycastHit hit;
        LayerMask lmask = 1 << 9;// 地形层
        
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Physics.Raycast(ray, out hit, Mathf.Infinity, lmask.value);
        if (hit.transform != null)
        {
            mousePointObj = hit.transform.gameObject;
            // 修改选中位置的颜色
            if (mousePointObj.tag == "Grid")
            {
                mousePointObj.GetComponent<Renderer>().material.color = Color.yellow;
                if (mousePointObj.name != mousePointObjName && mousePointObjName != "")
                {
                    GameObject.Find(mousePointObjName).GetComponent<Renderer>().material.color = Color.white;
                }
                mousePointObjName = mousePointObj.name;
            }
            else
            {
                if (mousePointObj.name != mousePointObjName && mousePointObjName != "")
                {
                    GameObject.Find(mousePointObjName).GetComponent<Renderer>().material.color = Color.white;
                }
            }
        }

        // 没有建筑以及在可释放范围内
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log(isLocalPlayer);
            if (mousePointObj.tag == "Grid")
            {
                mousePos = mousePointObj.transform.position;
                SpawnManager.instance.CmdBomb(mousePos);
                this.GetComponent<Resource>().UseCoin(new Barrack().price);

                if ((GameJudge.instance.Player1.transform.position.x < 0 && mousePos.x < 0) || (GameJudge.instance.Player1.transform.position.x > 0 && mousePos.x > 0))
                {
                    mask = 1 << 12;
                }
                else
                {
                    mask = 1 << 11;
                }
                Debug.Log(mask.value);
                Collider[] others = Physics.OverlapSphere(mousePos, 6.0f, mask.value);//获取所有碰撞体
                Soldier soldier;
                Debug.Log(others.Length);
                for (int i = 0; i < others.Length; i++)
                {
                    if ((soldier = others[i].GetComponent<Soldier>()))
                    {
                        soldier.BeAttacked(DamageNmerical);
                    }
                }
                // TODO: 释放技能消耗金币
                mousePointObj.GetComponent<Renderer>().material.color = Color.white;
                gridGroup.SetActive(false);

                Debug.Log("CreatBoom");
                // TODO: 利用网络释放技能
                

                Loadingbar.fillAmount = 0;
                BombSkillisActive = false;
            }
        }
        // 取消释放分支
        if (Input.GetMouseButtonDown(1))
        {
            gridGroup.SetActive(false);
            BombSkillisActive = false;
            return;
        }
    }
    
}
